package cn.tedu.mall.order.service.impl;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.pojo.domain.CsmallAuthenticationInfo;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.order.mapper.OmsCartMapper;
import cn.tedu.mall.order.service.IOmsCartService;
import cn.tedu.mall.pojo.order.dto.CartAddDTO;
import cn.tedu.mall.pojo.order.dto.CartUpdateDTO;
import cn.tedu.mall.pojo.order.model.OmsCart;
import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OmsCartServiceImpl implements IOmsCartService {


    @Autowired
    private OmsCartMapper omsCartMapper;

    @Override
    public void addCart(CartAddDTO cartDTO) {
        Long userId = getUserId();
        OmsCart omsCart=omsCartMapper.selectExistsCart(userId, cartDTO.getSkuId());
        if(omsCart==null){
            /*此时omsCart是null，根本都没有各个属性名和对应的值➡再实例化一下*/
            omsCart=new OmsCart();
            BeanUtils.copyProperties(cartDTO,omsCart);
            omsCart.setUserId(userId);
            /*没值的属性在其它方法中处理*/
            omsCartMapper.saveCart(omsCart);
        }else{
            /*sql语句是直接改值，所以需要提前加好*/
            omsCart.setQuantity(omsCart.getQuantity()+cartDTO.getQuantity());
            omsCartMapper.updateQuantityById(omsCart);
        }



    }



    @Override
    /*分页查询*/
    public JsonPage<CartStandardVO> listCarts(Integer page, Integer pageSize) {

        Long userId = getUserId();
        /*传进来的*/
        PageHelper.startPage(page,pageSize);

        List<CartStandardVO> list = omsCartMapper.selectCartsByUserId(userId);

        /*在list的基础上多了分页信息如总页数，总条数等*/
        return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public void removeCart(Long[] ids) {
        int rows=omsCartMapper.deleteCartsByIds(ids);
        if(rows==0){
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND,"您要删除的商品已经删除了！");
        }
    }

    @Override
    public void removeAllCarts() {
        Long userId=getUserId();
        int rows=omsCartMapper.deleteCartsByUserId(userId);
        if (rows==0){
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND,"您的购物车是空哒~");
        }
    }

    /*做订单➡删购物车，与上面分开写了*/
    @Override
    public void removeUserCarts(OmsCart omsCart) {
        omsCartMapper.deleteCartByUserIdAndSkuId(omsCart);
    }

    @Override
    public void updateQuantity(CartUpdateDTO cartUpdateDTO) {
        OmsCart omsCart = new OmsCart();
        BeanUtils.copyProperties(cartUpdateDTO,omsCart);
        omsCartMapper.updateQuantityById(omsCart);
    }



    public CsmallAuthenticationInfo getUserInfo() {
        UsernamePasswordAuthenticationToken authenticationToken=
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        if(authenticationToken==null){
            throw new CoolSharkServiceException(ResponseCode.UNAUTHORIZED,"您还未登录");
        }
        /*如果没找到可以试试getPrincipal方法，具体要看当时存到哪里*/
        CsmallAuthenticationInfo csmallAuthenticationInfo=(CsmallAuthenticationInfo) authenticationToken.getCredentials();

        return csmallAuthenticationInfo;
    }

    /*非自动生成，由于需要现写的，从SpringSecurity上下文中获取*/
    public Long getUserId() {
        return getUserInfo().getId();
    }
}
